// Copyright@ChenChao


#include "Animation/CAnimInstance.h"

#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"

void UCAnimInstance::NativeInitializeAnimation()
{
	Super::NativeInitializeAnimation();
	OwnerCharacter = Cast<ACharacter>(TryGetPawnOwner());
	if (OwnerCharacter)
	{
		OwnerMovementComponent = OwnerCharacter->GetCharacterMovement();
	}
}

void UCAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
	Super::NativeUpdateAnimation(DeltaSeconds);
	if (OwnerCharacter)
	{
		//获取速度值
		Speed = OwnerCharacter->GetVelocity().Length();		
		// 获取身体旋转
		FRotator BodyRotation = OwnerCharacter->GetActorRotation();
		//计算当前身体旋转和上一帧身体旋转的差值
		FRotator BodyRotationDelta = UKismetMathLibrary::NormalizedDeltaRotator(BodyRotation,BodyPreviousRotation);
		BodyPreviousRotation = BodyRotation;

		//计算偏航速度
		YawSpeed = BodyRotationDelta.Yaw / DeltaSeconds;
		//平滑偏航速度
		SmoothedYawSpeed = UKismetMathLibrary::FInterpTo(SmoothedYawSpeed, YawSpeed, DeltaSeconds, InterpSpeed);

		//获取视角旋转偏移
		FRotator ControlRotation = OwnerCharacter->GetBaseAimRotation();
		LookRotationOffset = UKismetMathLibrary::NormalizedDeltaRotator(ControlRotation,BodyRotation);
		
	}
	if (OwnerMovementComponent)
	{
		//是否跳跃
		bIsJumpping = OwnerMovementComponent->IsFalling();
	}
}

void UCAnimInstance::NativeThreadSafeUpdateAnimation(float DeltaSeconds)
{
	Super::NativeThreadSafeUpdateAnimation(DeltaSeconds);
}
